######### Notes ###########

# Demographics: All
#               Male, Female
#               16-24, 25-49, 50-64, 65+

# Other demographics not included
## Race (but non white categories very small n...)
## Class

# Ignoring security behaviours and attitudes towards the police and criminal justice system


######### CSEW ###########

library(tidyverse)
library(haven)

setwd("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Originals/CSEW") 


read_sav("CSEW_1982.sav") %>% # weighting scheme very hard to interpret
  select(sex, age, weighta, race,
         feelsafe, fearsnat, fearcth2, fearbur3, fearbur4) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(age = na_if(age, -7)) %>%
  mutate(age = na_if(age, 99)) %>%
  mutate(safewalkdark = ifelse(feelsafe %in% c(NA_integer_, 9), NA_integer_,
                                    tolower(as.character(as_factor(feelsafe))))) %>%
  mutate(race = case_match(race, 1 ~ 'white', 2 ~ 'black', 3 ~ 'asian', c(4,5) ~ 'other')) %>%
  rename(weight = weighta) %>%
  select(gender, age, race, weight,
         safewalkdark) %>%
  mutate(year = 1982) -> surveydata




read_sav("CSEW_1984.sav") %>% # weighting scheme very hard to interpret
  mutate(gender = case_when(D5A1 == 1 ~ 'Male', D5A1 == 2 ~ "Female")) %>%
  mutate(age = na_if(D5A2, -7)) %>%
  mutate(age = na_if(age, 99)) %>%
  mutate(race = case_match(RACE, 1 ~ 'white',
                           2 ~ 'black',
                           3 ~ 'asian',
                           4 ~ 'other')) %>%
  mutate(safewalkdark = ifelse(M7A %in% c(NA_integer_, -7, 9), NA_integer_,
                               tolower(as.character(as_factor(M7A))))) %>%
  mutate(across(starts_with('M8'),
                ~if_else(. %in% c(NA_integer_, -7, 8,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M8'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(starts_with('M9A'),
                ~if_else(. %in% c(NA_integer_, -7, 8,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M9A'),
                ~case_match(.,
                            1 ~ 'Certain to',
                            2 ~ 'Very likely',
                            3 ~ 'Fairly likely',
                            4 ~ 'Fairly unlikely',
                            5 ~ 'Not at all likely',
                            6 ~ 'Certain not to',
                            7 ~ 'DK'))) %>%
  mutate(across(starts_with('M10'),
                ~if_else(. %in% c(NA_integer_, -7, 8,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M10'),
                ~case_match(.,
                            1 ~ 'Very common',
                            2 ~ 'Fairly common',
                            3 ~ 'Not very common',
                            4 ~ 'Not at all common',
                            5 ~ 'DK'))) %>%
  rename(weight = WEIGHTA, 
         worryburg = M8A, worrymug = M8B, worryrape = M8C, worrymolest = M8D, worryvand = M8E, worryattack = M8F, worryinsult = M8G,
         likelyvand = M9A1, likelymug = M9A2, likelyattack = M9A3, likelyburg = M9A4, likelyrape = M9A5, likelymolest = M9A6,
         commonnoisy = M10A, commongraff = M10B, commonteen = M10C, commondrunk = M10D, commonlitter = M10E) %>%
  select(gender, age, race, weight,
         safewalkdark, 
         worryburg, worrymug, worryrape, worrymolest, worryvand, worryattack, worryinsult,
         likelyvand, likelymug, likelyattack, likelyburg, likelyrape, likelymolest,
         commonnoisy, commongraff, commonteen, commondrunk, commonlitter) %>%
  mutate(year = 1984) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_1988.sav") %>% # weighting scheme very hard to interpret
  filter(BOOSTWGT == 1) %>%
  mutate(gender = case_when(D8A1 == 1 ~ 'Male', D8A1 == 2 ~ "Female")) %>%
  mutate(age = na_if(D8A2, -7)) %>%
  mutate(age = na_if(age, 99)) %>%
  mutate(race = case_match(D14, 1 ~ "white", 2 ~ "black", c(3,4,5,7) ~ 'asian',
                           c(6,8) ~ 'other')) %>%
  mutate(safewalkdark = ifelse(M4A %in% c(NA_integer_, -7, 9), NA_integer_,
                               tolower(as.character(as_factor(M4A))))) %>%
  mutate(safehomenight = ifelse(M4B %in% c(NA_integer_, -7, 9), NA_integer_,
                               tolower(as.character(as_factor(M4B))))) %>%
  mutate(across(starts_with('M5'),
                ~if_else(. %in% c(NA_integer_, -7, 8,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M5'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(starts_with('M6'),
                ~if_else(. %in% c(NA_integer_, -7, 8,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M6'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(starts_with('M8'),
                ~if_else(. %in% c(NA_integer_, -7, 8,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M8'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = WEIGHTA, 
         worryburg = M5A, worrymug = M5B, worryrape = M5C, worryvand = M5D, worrycarstolen = M5E, worrycartheft = M5F, worrycarvand = M5G,
         likelyburg = M6A, likelymug = M6B, likelyrape = M6C, likelyvand = M6D, likelycarstolen = M6E, likelycartheft = M6F, likelycarvand = M6G,
         commonnoisy = M8A, commongraff = M8B, commonteen = M8C, commondrunk = M8D, commonlitter = M8E, commonpoorcond = M8F, commonvand = M8G) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape, worryvand, worrycarstolen, worrycartheft, worrycarvand,
         likelyburg, likelymug, likelyrape, likelyvand, likelycarstolen, likelycartheft, likelycarvand,
         commonnoisy, commongraff, commonteen, commondrunk, commonlitter, commonpoorcond, commonvand) %>%
  mutate(year = 1988) %>%
bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_1992.sav") %>% # weighting scheme very hard to interpret
  filter(WEIGHTEM == 1) %>%
  mutate(gender = case_when(D9A1 == 1 ~ 'Male', D9A1 == 2 ~ "Female")) %>%
  mutate(age = na_if(D9A2, -7)) %>%
  mutate(age = na_if(age, 99)) %>%
  mutate(race = case_match(D20, 1 ~ "white", c(2,3,4) ~ "black", c(5,6,7) ~ "asian",
                           c(8,9) ~ 'other')) %>%
  mutate(safewalkdark = ifelse(M4A %in% c(NA_integer_, -7, 9), NA_integer_,
                               tolower(as.character(as_factor(M4A))))) %>%
  mutate(safehomenight = ifelse(M4B %in% c(NA_integer_, -7, 9), NA_integer_,
                                tolower(as.character(as_factor(M4B))))) %>%
  mutate(across(starts_with('M5'),
                ~if_else(. %in% c(NA_integer_, -7, 5,9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M5'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(starts_with('M7'),
                ~if_else(. %in% c(NA_integer_, -7, 9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M7'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(starts_with('M8'),
                ~if_else(. %in% c(NA_integer_, -7, 9,10,11), NA_integer_, .))) %>%
  mutate(across(starts_with('M8'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = WEIGHTA, 
         worryburg = M5A, worrymug = M5B, worrycarstolen = M5C, worrycartheft = M5D, worryrape = M5E,
         crimeincr = M7,
         probnoisy = M8A, probteen = M8B, probdrunk = M8C, problitter = M8D, probvand = M8E, probraceattacks = M8F, probdrugs = M8G) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worrycarstolen, worrycartheft, worryrape,
         crimeincr,
         probnoisy, probteen, probdrunk, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 1992) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_1994.sav") %>% # weighting scheme very hard to interpret
  filter(SUBAD == 1) %>%
  mutate(gender = case_when(SEX == 1 ~ 'Male', SEX == 2 ~ "Female")) %>%
  mutate(age = na_if(AGE, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(race = case_match(ETHNICID, 1 ~ "white", c(2,3,4) ~ "black", c(5,6,7) ~ "asian",
                           c(8,9) ~ 'other')) %>%
  mutate(safewalkdark = ifelse(WALKDARK %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(WALKDARK))))) %>%
  mutate(safehomenight = ifelse(HOMEALON %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(HOMEALON))))) %>%
  mutate(across(c('WBURGL','WMUGGED','WRAPED','WCARSTOL','WFROMCAR','WINSULT','WRACEATT'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('WBURGL','WMUGGED','WRAPED','WCARSTOL','WFROMCAR','WINSULT','WRACEATT'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('CRIMERAT'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('CRIMERAT'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('NOISNEIG','TEENHANG','DRUNKS','RUBBISH','VANDALS','RACEATT','DRUGUSE'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('NOISNEIG','TEENHANG','DRUNKS','RUBBISH','VANDALS','RACEATT','DRUGUSE'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = WEIGHTA, 
         worryburg = WBURGL, worrymug = WMUGGED, worryrape = WRAPED, worrycarstolen = WCARSTOL, worrycartheft = WFROMCAR, worryinsult = WINSULT, worryraceattacks = WRACEATT,
         crimeincr = CRIMERAT,
         probnoisy = NOISNEIG, probteen = TEENHANG, probdrunk = DRUNKS, problitter = RUBBISH, probvand = VANDALS, probraceattacks = RACEATT, probdrugs = DRUGUSE) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape, worrycarstolen, worrycartheft, worryinsult, worryraceattacks,
         crimeincr,
         probnoisy, probteen, probdrunk, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 1994) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_1996.sav") %>% # weighting scheme very hard to interpret
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(race = case_match(ethnicid, 1 ~ "white", c(2,3,4) ~ "black", c(5,6,7) ~ "asian",
                           c(8,9) ~ 'other')) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('crimerat','sentsev','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','drunks','rubbish','vandals','raceatt','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','drunks','rubbish','vandals','raceatt','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = weighta, 
         worryburg = wburgl, worrymug = wmugged, worryrape = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,
         crimeincr = crimerat, senttoolenient = sentsev, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, probdrunk = drunks, problitter = rubbish, probvand = vandals, probraceattacks = raceatt, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape, worrycarstolen, worrycartheft, worryraceattacks,
         crimeincr, senttoolenient, crimeincrUK,
         probnoisy, probteen, probdrunk, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 1996) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_1998.sav") %>% # weighting scheme very hard to interpret
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(race = case_match(ethnicid, 1 ~ "white", c(2,3,4) ~ "black", c(5,6,7) ~ "asian",
                           c(8,9) ~ 'other')) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife'),
                ~if_else(. %in% c(NA_integer_, -7, 99), NA_integer_, .))) %>%
  mutate(across(c('quallife'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                    ifelse(causem %in% c(NA_integer_, 99), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','drunks','rubbish','vandals','raceatt','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','drunks','rubbish','vandals','raceatt','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = weighta, 
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt, worryattack = wattack,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, probdrunk = drunks, problitter = rubbish, probvand = vandals, probraceattacks = raceatt, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, probdrunk, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 1998) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2000.sav") %>% # weighting scheme very hard to interpret
  filter(sample == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethnicid, 1 ~ "white", c(2,3,4) ~ "black", c(5,6,7) ~ "asian",
                           c(8,9,10) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 99), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(causem = ifelse(is.na(causem), causem1, causem)) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 99), NA_integer_, 0))) %>%
    mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehatt','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehatt','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = weighta, 
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehatt, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2000) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2001-02.sav") %>% # weighting makes sense from here on...
  filter(samptype == 1) %>% 
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins, 
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2001) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2002-03.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins, 
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2002) %>%
  bind_rows(surveydata, .) -> surveydata
  



read_sav("CSEW_2003-04.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins, 
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2003) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2004-05.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2004) %>% 
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_2005-06.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2005) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2006-07.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2006) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_2007-08.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimerat','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimerat, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2007) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_2008-09.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2','percon'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimrat2, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         crimeincr2 = perclc2, crimeincrUK2 = percll3, crimconincr = percon,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         crimeincr2, crimeincrUK2, crimconincr,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2008) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2009-10.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2', 'percon'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimrat2, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         crimeincr2 = perclc2, crimeincrUK2 = percll3, crimconincr = percon,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         crimeincr2, crimeincrUK2, crimconincr,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2009) %>% 
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2010-11.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2', 'percon'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimrat2, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         crimeincr2 = perclc2, crimeincrUK2 = percll3, crimconincr = percon,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         crimeincr2, crimeincrUK2, crimconincr,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2010) %>%
  bind_rows(surveydata, .) -> surveydata



  
read_sav("CSEW_2011-12.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack','winsult'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent = ifelse(causem %in% c(1,9), 1,
                                   ifelse(causem %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('crimrat2','sentsev', 'juvsen','crimuk'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2', 'percon'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','racehat2','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = wtm2ins,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack, worryinsult = winsult,
         crimeincr = crimrat2, senttoolenient = sentsev, juvtoolenient = juvsen, crimeincrUK = crimuk,
         crimeincr2 = perclc2, crimeincrUK2 = percll3, crimconincr = percon,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probraceattacks = racehat2, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack, worryinsult,
         quallife, qualif2, crimcausepolsent,
         crimeincr, senttoolenient, juvtoolenient, crimeincrUK,
         crimeincr2, crimeincrUK2, crimconincr,
         probnoisy, probteen, problitter, probvand, probraceattacks, probdrugs) %>%
  mutate(year = 2011) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_2012-13.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent2 = ifelse(cause2m %in% c(1,9), 1,
                                   ifelse(cause2m %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('sentsev', 'juvsen'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('sentsev', 'juvsen'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         senttoolenient = sentsev, juvtoolenient = juvsen,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2, crimcausepolsent2,
         senttoolenient, juvtoolenient,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2012) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2013-14.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent2 = ifelse(cause2m %in% c(1,9), 1,
                                   ifelse(cause2m %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('sentsev'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('sentsev'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         senttoolenient = sentsev,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2, crimcausepolsent2,
         senttoolenient,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2013) %>%
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_2014-15.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent2 = ifelse(cause2m %in% c(1,9), 1,
                                   ifelse(cause2m %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('sentsev'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('sentsev'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         senttoolenient = sentsev,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2, crimcausepolsent2,
         senttoolenient,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2014) %>% 
  bind_rows(surveydata, .) -> surveydata




read_sav("CSEW_2015-16.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(safehomenight = ifelse(homealon %in% c(NA_integer_, -7, 8), NA_integer_,
                                tolower(as.character(as_factor(homealon))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(crimcausepolsent3 = ifelse(cause3m %in% c(1,9), 1,
                                   ifelse(cause3m %in% c(NA_integer_, 98), NA_integer_, 0))) %>%
  mutate(across(c('sentsev','juvsen'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('sentsev','juvsen'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         senttoolenient = sentsev, juvtoolenient = juvsen,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark, safehomenight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2, crimcausepolsent3,
         senttoolenient, juvtoolenient,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2015) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2016-17.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('sentsev'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('sentsev'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         senttoolenient = sentsev,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         senttoolenient,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2016) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2017-18.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(across(c('sentsev','juvsen'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('sentsev','juvsen'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         senttoolenient = sentsev, juvtoolenient = juvsen,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2,
         senttoolenient, juvtoolenient,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2017) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("CSEW_2018-19.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2018) %>% 
  bind_rows(surveydata, .) -> surveydata


read_sav("CSEW_2019-20.sav") %>%
  filter(samptype == 1) %>%
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(race = case_match(ethgrp2a, 1 ~ "white", 3 ~ 'asian', 4 ~ 'black', c(2,5) ~ 'other')) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(walkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(walkdark))))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~if_else(. %in% c(NA_integer_, -7, 5, 8), NA_integer_, .))) %>%
  mutate(across(c('wburgl','wmugged','wraped','wcarstol','wfromcar','wraceatt','wattack'),
                ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('quallife','qualif2'),
                ~if_else(. %in% c(NA_integer_, -7, 98), NA_integer_, .))) %>%
  mutate(across(c('quallife','qualif2'),
                ~case_match(.,
                            1 ~ '1 - no effect',
                            10 ~ '10 - total effect',
                            .default = as.character(.)))) %>%
  mutate(across(c('percll3', 'perclc2'), ~tolower(as.character(as_factor(.))))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~if_else(. %in% c(NA_integer_, -7, 8), NA_integer_, .))) %>%
  mutate(across(c('noisneig','teenhang','rubbish','vandals','druguse'),
                ~tolower(as.character(as_factor(.))))) %>%
  rename(weight = C11IndivWgt,
         worryburg = wburgl, worrymug = wmugged, worryrape2 = wraped, worrycarstolen = wcarstol, worrycartheft = wfromcar, worryraceattacks = wraceatt,  worryattack = wattack,
         crimeincr2 = perclc2, crimeincrUK2 = percll3,
         probnoisy = noisneig, probteen = teenhang, problitter = rubbish, probvand = vandals, probdrugs = druguse) %>%
  select(gender, age, race, weight,
         safewalkdark,
         worryburg, worrymug, worryrape2, worrycarstolen, worrycartheft, worryraceattacks, worryattack,
         quallife, qualif2,
         crimeincr2, crimeincrUK2,
         probnoisy, probteen, problitter, probvand, probdrugs) %>%
  mutate(year = 2019) %>%
  bind_rows(surveydata, .) -> surveydata


read_sav("CSEW_2020-21.sav") %>% # no race variable
  mutate(gender = case_when(sex == 1 ~ 'Male', sex == 2 ~ "Female")) %>%
  mutate(age = na_if(age, 998)) %>%
  mutate(age = na_if(age, 999)) %>%
  mutate(safewalkdark = ifelse(cvwalkdark %in% c(NA_integer_, -7, 8), NA_integer_,
                               tolower(as.character(as_factor(cvwalkdark))))) %>%
  rename(weight = c11indivwgt) %>%
  select(gender, age, weight,
         safewalkdark) %>%
  mutate(year = 2020) %>%
  bind_rows(surveydata, .) -> surveydata


write_csv(surveydata, "/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/CSEW_allrepeatedcrimequestions.csv")

